package com.liquidcode.cellular;

import android.app.Activity;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.opengl.Visibility;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.MotionEvent;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;

public class Cellular_androidActivity extends Activity {
    /** Called when the activity is first created. */
	private GLSurface m_surface = null; 
	private SensorManager mSensorManager = null; 
	private Sensor mAccelerometer = null;
	private boolean mInitAttackMenu = false;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mInitAttackMenu = false;
        // make activity full screen 
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        setContentView(R.layout.game_view);
        m_surface = (GLSurface) findViewById(R.id.gLSurface1);

        GameInfo.getInstance().GameStep = 1.0f;
        
        String[] attacks = getResources().getStringArray(R.array.attack_array);
        CheckedTextView[] textArr = new CheckedTextView[attacks.length];
        for( int i = 0; i < attacks.length; ++i )
        {
        	textArr[i] = new CheckedTextView(getApplicationContext());
        	textArr[i].setText(attacks[i].toString());
        }

        final Button attackButton = (Button)findViewById(R.id.attack_button);
        final ListView attackList = (ListView)findViewById(R.id.attack_list);
        final Button attackOkayButton = (Button)findViewById(R.id.attack_ok_button);
        final Button shieldButton = (Button)findViewById(R.id.shield_button);
        attackList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, attacks));
        attackList.setVisibility(View.INVISIBLE);
        attackList.setBackgroundColor(Color.BLACK);
        attackList.setSelection(CAttackManager.SINGLE_SHOT);
        attackList.setItemChecked(CAttackManager.SINGLE_SHOT, true);
        attackOkayButton.setVisibility(Button.INVISIBLE);
        
        /* Item clicked callback */
        attackList.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				if( !mInitAttackMenu ) {
					mInitAttackMenu = true;
				}
				CAttackManager.getInstance().SetCurrentAttackInfo(arg2);
				Log.d("Current Attack Type: ", String.format("%d", CAttackManager.getInstance().CurrentAttackType));
			}
		});
        
        /* Callback when an attack is long clicked (descriptions) */
        attackList.setOnItemLongClickListener(new OnItemLongClickListener() {

			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				Log.d("ON LONG CLICK ATTACK LIST", "ON LONG CLICK CALLED!");
				
				return false;
			}
		});
        
///////// Attack Button
        attackButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(GameInfo.getInstance().GameStep == 1.0f )
				{
					GameInfo.getInstance().PauseGame(true);
					attackList.setVisibility(ListView.VISIBLE);
					attackOkayButton.setVisibility(Button.VISIBLE);
					attackButton.setVisibility(Button.INVISIBLE);
					shieldButton.setVisibility(Button.INVISIBLE);
				}

			}
		});
        
        
///////////// Attack Okay Button
        attackOkayButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				GameInfo.getInstance().PauseGame(false);
				attackOkayButton.setVisibility(Button.INVISIBLE);
				attackButton.setVisibility(Button.VISIBLE);
				shieldButton.setVisibility(Button.VISIBLE);
				attackList.setVisibility(ListView.INVISIBLE);
			}
		});
        
///////////// Shield Button
        shieldButton.setOnTouchListener(new OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				
				switch( event.getAction() )
				{
				case MotionEvent.ACTION_DOWN:
				{
					GameInfo.getInstance().ShieldPressed = true;
					break;
				}
				case MotionEvent.ACTION_UP:
				{
					GameInfo.getInstance().ShieldPressed = false;
					break;
				}
				
				
				}
				
				return false;
			}
		});
        
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent ev)
    {
    	if( keyCode == KeyEvent.KEYCODE_BACK )
    	{
    		GameInfo.getInstance().PauseGame(false);
    	}
    	
    	super.onKeyDown(keyCode, ev);
    	return true;
    }
    
    @Override
    public void onResume()
    {
    	super.onResume();
    	if( m_surface.getRenderer().getGameThread() == null )
    	{
    		m_surface.getRenderer().mThread = new GameThread();
    		m_surface.getRenderer().mThread.start();
    		m_surface.getRenderer().mThread.running = true;	
    	}
    }
    
    
    @Override 
    public void onStop()
    {
    	super.onStop();
    	m_surface.getRenderer().mThread.running = false;
    	m_surface.getRenderer().stopThread();
    }
}
